home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODF R2 for CodeWarrior 10 / Sources / ODF / Found / FWCommon / FWPriMem.cpp next >
Encoding:
Text File  |  1996-09-26  |  4.0 KB  |  132 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPriMem.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef FWNEW_H
  13. #include "FWNew.h"
  14. #endif
  15.  
  16. #ifndef FWPRIMEM_H
  17. #include "FWPriMem.h"
  18. #endif
  19.  
  20. #ifndef SLPRIDEB_H
  21. #include "SLPriDeb.h"
  22. #endif
  23.  
  24. #ifdef FW_BUILD_MAC
  25. #pragma segment FWCommon
  26. #endif
  27.  
  28. //========================================================================================
  29. // set_new_handler
  30. //========================================================================================
  31.  
  32. // We (optionally) provide our own definition of set_new_handler mainly as a precaution against
  33. // ending up with a shared library version of set_new_handler.  ODF as a whole has been
  34. // written assuming that operator new will throw an exception if it fails to allocate
  35. // memory.  In a runtime environment such as OpenDoc, it's possible for multiple shared
  36. // libraries to exist in one process.  An exception thrown from code in one shared
  37. // library should not propogate outside of the shared library (since there is no agreed
  38. // binary interface for exceptions across shared libraries).  This implies that if
  39. // operator new throws exceptions then operator new cannot be implemented in a shared
  40. // library.  In actuality, it's the "new hander" that throws, so there must be one new
  41. // handler per shared library, and thus one version of set_new_handler per shared library.
  42. // This unit (FWPriMem.cpp) is intended to be statically linked into each shared library
  43. // built with ODF.
  44.  
  45. #if defined(FW_DONT_USE_STD_LIB_SET_NEW_HANDLER)
  46.  
  47.     static pfvv gNewHandler = 0;
  48.     
  49.     #ifndef _MSC_VER
  50.         pfvv set_new_handler(pfvv handler)
  51.         {
  52.             // See ARM, pp 280-81.
  53.             pfvv oldHandler = gNewHandler;
  54.             gNewHandler = handler;
  55.             return oldHandler;
  56.         }
  57.     #endif
  58.     
  59.     #ifdef _MSC_VER
  60.             
  61.         pfvv __cdecl set_new_handler(pfvv handler)
  62.         {
  63.             // See ARM, pp 280-81.
  64.             pfvv oldHandler = gNewHandler;
  65.             gNewHandler = handler;
  66.             return oldHandler;
  67.         }
  68.         
  69.         // [KVV] Because of the way Visual C++ 4.0 linker/library work,
  70.         //    we have to define the following stuff
  71.         
  72.         _PNH _set_new_handler(_PNH /* pnh */)
  73.         {
  74.             return 0;
  75.         }
  76.         
  77.         _PNH _query_new_handler()
  78.         {
  79.             return 0;
  80.         }
  81.         
  82.         extern "C" int _callnewh(size_t /* size */)
  83.         {
  84.             return 1;
  85.         }
  86.     
  87.     #endif
  88.  
  89. #endif
  90.  
  91. //========================================================================================
  92. // C++ Global operator new & delete
  93. //========================================================================================
  94.  
  95. //----------------------------------------------------------------------------------------
  96. // operator new
  97. //----------------------------------------------------------------------------------------
  98. void *operator new(size_t size)
  99. {
  100.     void* p;
  101.     
  102.     while ((p = FW_PrimitiveAllocateBlock(size)) == NULL)
  103.     {
  104.         pfvv newHandler;
  105.         // this is just a cheap way to get the newHandler function pointer
  106.         set_new_handler(newHandler = set_new_handler(0));
  107.         FW_PRIV_ASSERT(newHandler != NULL);
  108.         // ODF is written assuming that operator new will not return if it fails to
  109.         // allocate memory.  It is assumed that a newHandler will be installed which
  110.         // throws an exception.  ODF installs a newHandler in a higher layer (FWODExce)
  111.         // so a newHandler will exist unless a developer chooses to intentionally
  112.         // deinstall the newHandler by calling ::set_new_handler(0).  Doing so will
  113.         // break ODF's error handling strategy and is considered illegal.  Note
  114.         // that developers are free to install their own newHandler.
  115.         if (!newHandler)
  116.             return 0;
  117.         else
  118.             newHandler();
  119.     }
  120.  
  121.     return p;
  122. }
  123.  
  124. //----------------------------------------------------------------------------------------
  125. // operator delete
  126. //----------------------------------------------------------------------------------------
  127. void operator delete(void *p)
  128. {
  129.     FW_PrimitiveFreeBlock(p);
  130. }
  131.  
  132.